{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": "true"
   },
   "source": [
    "# Table of Contents\n",
    " <p><div class=\"lev1\"><a href=\"#Convex-Hull-Tests\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Convex Hull Tests</a></div><div class=\"lev1\"><a href=\"#Min-Area-Rectangle-Tests\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Min Area Rectangle Tests</a></div><div class=\"lev1\"><a href=\"#Diameter-MVBB-Tests\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Diameter MVBB Tests</a></div><div class=\"lev1\"><a href=\"#MVBB-Tests\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>MVBB Tests</a></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:45:44.257560",
     "start_time": "2017-11-12T20:45:43.593822"
    },
    "_draft": {
     "init_cell": true
    },
    "code_folding": [
     1
    ],
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/zfmgpu/.config/matplotlib'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys,os,imp,re\n",
    "import math\n",
    "import numpy as np\n",
    "import matplotlib as mpl\n",
    "\n",
    "import matplotlib.cm as cm\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import matplotlib.colors as colors\n",
    "import matplotlib.cm as cmx\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "\n",
    "mpl.rcParams['figure.figsize']=(6.0,4.0)    #(6.0,4.0)\n",
    "mpl.rcParams['font.size']=10                #10 \n",
    "mpl.rcParams['savefig.dpi']=400             #72 \n",
    "mpl.rcParams['figure.subplot.bottom']=.1    #.125\n",
    "\n",
    "plt.rc('font', family='serif')\n",
    "plt.rc('text', usetex=True)\n",
    "\n",
    "#inline Shit\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format='svg'\n",
    "%config InlineBackend.rc = {'figure.facecolor': 'white', 'figure.subplot.bottom': 0.125, 'figure.edgecolor': 'white', 'savefig.dpi': 400, 'figure.figsize': (12.0, 8.0), 'font.size': 10}\n",
    "\n",
    "#GUi shit\n",
    "%matplotlib tk\n",
    "mpl.get_configdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:45:44.485077",
     "start_time": "2017-11-12T20:45:44.398022"
    },
    "_draft": {
     "init_cell": true
    },
    "code_folding": [
     1,
     29,
     37,
     47
    ],
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\n",
    "def plotCube(ax,minP = np.array([-1.0,-1.0,-1.0]), maxP=np.array([1.0,1.0,1.0]),\n",
    "             trans= np.array([0.0,0.0,0.0]),rotationMatrix=np.diag([1,1,1])):\n",
    "    from itertools import product, combinations\n",
    "    r = [-1, 1]\n",
    "    \n",
    "    centerPos = (maxP + minP)/2.0;\n",
    "    #print(centerPos)\n",
    "    extent = (maxP - minP)/2.0;\n",
    "    \n",
    "    points = np.array([(-1, -1, -1),\n",
    "                     (-1, -1, 1),\n",
    "                     (-1, 1, -1),\n",
    "                     (-1, 1, 1),\n",
    "                     (1, -1, -1),\n",
    "                     (1, -1, 1),\n",
    "                     (1, 1, -1),\n",
    "                     (1, 1, 1)]);\n",
    "    \n",
    "    for s, e in combinations(points, 2):\n",
    "        if np.sum(np.abs(s-e)) == r[1]-r[0]: # no diagonal lines\n",
    "            p1 = np.array(s,dtype=float); p2 = np.array(e,dtype=float);\n",
    "            #scale points\n",
    "            p1*=extent; p2*=extent;\n",
    "            #rotate and translate points\n",
    "            p1 = rotationMatrix.dot(p1 + centerPos) + trans;\n",
    "            p2 = rotationMatrix.dot(p2+centerPos) + trans;\n",
    "            ax.plot3D(*zip(p1,p2), color=\"b\")\n",
    "\n",
    "def plotAxis(ax,centerPos,A_IK,plotAxisScale=1):\n",
    "        for i,c in zip([0,1,2],['r','g','b']):\n",
    "            I_eK_i = A_IK[:,i];\n",
    "            lines = list(zip(centerPos,plotAxisScale*I_eK_i+centerPos))\n",
    "            v = Arrow3D(*lines, mutation_scale=50, lw=1, arrowstyle=\"-|>\", color=c);\n",
    "            ax.plot3D(*lines, color=c)\n",
    "            ax.add_artist(v);\n",
    "\n",
    "def plotAxis2d(ax,centerPos,u,v,plotAxisScale=1):\n",
    "    \n",
    "    x = np.vstack((centerPos,plotAxisScale*u+centerPos))\n",
    "    y = np.vstack((centerPos,plotAxisScale*v+centerPos))\n",
    "    ax.plot(x.T[0],x.T[1],'r',lw=2)\n",
    "    ax.plot(y.T[0],y.T[1],'b',lw=2)\n",
    "        \n",
    "            \n",
    "from matplotlib.patches import FancyArrowPatch\n",
    "from mpl_toolkits.mplot3d import proj3d\n",
    "class Arrow3D(FancyArrowPatch):\n",
    "        def __init__(self, xs, ys, zs, *args, **kwargs):\n",
    "            FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)\n",
    "            self._verts3d = xs, ys, zs\n",
    "     \n",
    "        def draw(self, renderer):\n",
    "            xs3d, ys3d, zs3d = self._verts3d\n",
    "            xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)\n",
    "            self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))\n",
    "            FancyArrowPatch.draw(self, renderer)  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:45:45.255650",
     "start_time": "2017-11-12T20:45:45.247441"
    },
    "_draft": {
     "init_cell": true
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def axisEqual3D(ax):\n",
    "    extents = np.array([getattr(ax, 'get_{}lim'.format(dim))() for dim in 'xyz'])\n",
    "    sz = extents[:,1] - extents[:,0]\n",
    "    centers = np.mean(extents, axis=1)\n",
    "    maxsize = max(abs(sz))\n",
    "    r = maxsize/2\n",
    "    for ctr, dim in zip(centers, 'xyz'):\n",
    "        getattr(ax, 'set_{}lim'.format(dim))(ctr - r, ctr + r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:45:45.977048",
     "start_time": "2017-11-12T20:45:45.964540"
    },
    "_draft": {
     "init_cell": true
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# get all files\n",
    "def loadFiles(folderPath,filePathRegex, keyNames):\n",
    "    files = [ os.path.join(folderPath,f) for f in os.listdir(folderPath) if os.path.isfile( os.path.join(folderPath,f) ) ]\n",
    "\n",
    "    filePaths=dict();\n",
    "    for f in files:\n",
    "        res = re.match(filePathRegex,f)\n",
    "        if(res):\n",
    "            key = res.group(1)\n",
    "            filePaths[key]= dict( [ (keyN, g) for keyN,g in zip(keyNames,res.groups()) ] )\n",
    "            filePaths[key][\"path\"] = f\n",
    "    return filePaths;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:45:46.499062",
     "start_time": "2017-11-12T20:45:46.483501"
    },
    "_draft": {
     "init_cell": true
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import struct\n",
    "def readPointsMatrixBinary(filePath):\n",
    "    f = open(filePath, \"br\")\n",
    "    # read header (rows,columns)\n",
    "    (bigEndian,) = struct.unpack(\"?\",f.read(1));\n",
    "    formatStringBO = \"<\"; # little endian\n",
    "    dtype = np.dtype(\"<f8\")\n",
    "    if(bigEndian):\n",
    "        formatStringBO = \">\";\n",
    "        dtype = np.dtype(\">f8\")\n",
    "    \n",
    "    (rows,cols,nbytes) = struct.unpack(\"%sQQQ\" % formatStringBO ,  f.read(3*np.int64(0).nbytes))\n",
    "    print(\"Matrix Binary: \" , rows,cols,nbytes, \"big Endian:\", bigEndian)                           \n",
    "    return np.fromfile(f,dtype=dtype);\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convex Hull Tests\n",
    "================="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:57:51.892246",
     "start_time": "2017-11-12T20:57:45.050567"
    },
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PointsBadProjection4 {'name': 'PointsBadProjection4', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection4-double.bin'} {'name': 'PointsBadProjection4', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection4-double-Out.bin'}\n",
      "Matrix Binary:  2 16 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "Triangle {'name': 'Triangle', 'prec': 'double', 'path': './ConvexHullTest-Triangle-double.bin'} {'name': 'Triangle', 'prec': 'double', 'path': './ConvexHullTest-Triangle-double-Out.bin'}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/python3.4Env/lib/python3.4/site-packages/matplotlib/text.py:2130: UserWarning: You have used the `textcoords` kwarg, but not the `xytext` kwarg.  This can lead to surprising results.\n",
      "  warnings.warn(\"You have used the `textcoords` kwarg, but not \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "PointsOnCricle1000 {'name': 'PointsOnCricle1000', 'prec': 'double', 'path': './ConvexHullTest-PointsOnCricle1000-double.bin'} {'name': 'PointsOnCricle1000', 'prec': 'double', 'path': './ConvexHullTest-PointsOnCricle1000-double-Out.bin'}\n",
      "Matrix Binary:  2 1000 8 big Endian: False\n",
      "Matrix Binary:  2 1000 8 big Endian: False\n",
      "PointsBadProjection {'name': 'PointsBadProjection', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection-double.bin'} {'name': 'PointsBadProjection', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 14 8 big Endian: False\n",
      "PointsBadProjectionFilter {'name': 'PointsBadProjectionFilter', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjectionFilter-double.bin'} {'name': 'PointsBadProjectionFilter', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjectionFilter-double-Out.bin'}\n",
      "Matrix Binary:  2 34 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "Points2DRectFail {'name': 'Points2DRectFail', 'prec': 'double', 'path': './ConvexHullTest-Points2DRectFail-double.bin'} {'name': 'Points2DRectFail', 'prec': 'double', 'path': './ConvexHullTest-Points2DRectFail-double-Out.bin'}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/python3.4Env/lib/python3.4/site-packages/matplotlib/text.py:2130: UserWarning: You have used the `textcoords` kwarg, but not the `xytext` kwarg.  This can lead to surprising results.\n",
      "  warnings.warn(\"You have used the `textcoords` kwarg, but not \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Matrix Binary:  2 18 8 big Endian: False\n",
      "Matrix Binary:  2 8 8 big Endian: False\n",
      "Point {'name': 'Point', 'prec': 'double', 'path': './ConvexHullTest-Point-double.bin'} {'name': 'Point', 'prec': 'double', 'path': './ConvexHullTest-Point-double-Out.bin'}\n",
      "Matrix Binary:  2 1 8 big Endian: False\n",
      "Matrix Binary:  2 1 8 big Endian: False\n",
      "PointsBadProjection3 {'name': 'PointsBadProjection3', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection3-double.bin'} {'name': 'PointsBadProjection3', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection3-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 8 8 big Endian: False\n",
      "PointsBadProjection2 {'name': 'PointsBadProjection2', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection2-double.bin'} {'name': 'PointsBadProjection2', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection2-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 10 8 big Endian: False\n",
      "Line_2 {'name': 'Line_2', 'prec': 'double', 'path': './ConvexHullTest-Line_2-double.bin'} {'name': 'Line_2', 'prec': 'double', 'path': './ConvexHullTest-Line_2-double-Out.bin'}\n",
      "Matrix Binary:  2 2 8 big Endian: False\n",
      "Matrix Binary:  2 2 8 big Endian: False\n",
      "PointsBadProjection5 {'name': 'PointsBadProjection5', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection5-double.bin'} {'name': 'PointsBadProjection5', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection5-double-Out.bin'}\n",
      "Matrix Binary:  2 5 8 big Endian: False\n",
      "Matrix Binary:  2 4 8 big Endian: False\n",
      "Line4 {'name': 'Line4', 'prec': 'double', 'path': './ConvexHullTest-Line4-double.bin'} {'name': 'Line4', 'prec': 'double', 'path': './ConvexHullTest-Line4-double-Out.bin'}\n",
      "Matrix Binary:  2 5 8 big Endian: False\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Line3 {'name': 'Line3', 'prec': 'double', 'path': './ConvexHullTest-Line3-double.bin'} {'name': 'Line3', 'prec': 'double', 'path': './ConvexHullTest-Line3-double-Out.bin'}\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Matrix Binary:  2 2 8 big Endian: False\n",
      "PointsBadProjection6 {'name': 'PointsBadProjection6', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection6-double.bin'} {'name': 'PointsBadProjection6', 'prec': 'double', 'path': './ConvexHullTest-PointsBadProjection6-double-Out.bin'}\n",
      "Matrix Binary:  2 100 8 big Endian: False\n",
      "Matrix Binary:  2 8 8 big Endian: False\n",
      "PointsRandom10 {'name': 'PointsRandom10', 'prec': 'double', 'path': './ConvexHullTest-PointsRandom10-double.bin'} {'name': 'PointsRandom10', 'prec': 'double', 'path': './ConvexHullTest-PointsRandom10-double-Out.bin'}\n",
      "Matrix Binary:  2 10 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "PointsRandom14M {'name': 'PointsRandom14M', 'prec': 'double', 'path': './ConvexHullTest-PointsRandom14M-double.bin'} {'name': 'PointsRandom14M', 'prec': 'double', 'path': './ConvexHullTest-PointsRandom14M-double-Out.bin'}\n",
      "Matrix Binary:  2 42 8 big Endian: False\n"
     ]
    }
   ],
   "source": [
    "plt.close(\"all\")\n",
    "\n",
    "files = loadFiles(\"./\" , \".*ConvexHullTest-(.*?)-(\\w+)\\.bin\", (\"name\",\"prec\") );\n",
    "filesOut = loadFiles(\"./\" , \".*ConvexHullTest-(.*?)-(\\w+)-Out\\.bin\", (\"name\",\"prec\"));\n",
    "\n",
    "for i,f in files.items():\n",
    "    \n",
    "    fOut = filesOut[i]; print(i,f,fOut)\n",
    "    fig = plt.figure(\"ConvexHullTest\"+str(i),(10,10))\n",
    "    \n",
    "    if(i!=\"PointsRandom14M\"):\n",
    "        points = readPointsMatrixBinary(f[\"path\"]); points=np.reshape(points,(-1,2)) \n",
    "        \n",
    "    hullP =  readPointsMatrixBinary(fOut[\"path\"]); hullP=np.reshape(hullP,(-1,2))\n",
    "    hullP = np.vstack((hullP,hullP[0]))\n",
    "    \n",
    "    plt.plot(hullP.T[0],hullP.T[1],'b-o', ms=20, markerfacecolor='None')\n",
    "    \n",
    "    if(i!=\"PointsRandom14M\"):\n",
    "        plt.scatter(points.T[0],points.T[1],c='r')\n",
    "        if(len(points)<300):\n",
    "            nrRange = [(i,p[0],p[1])  for i,p in enumerate(points) ]\n",
    "            for x in nrRange:      \n",
    "                plt.annotate('%s' % x[0], xy=x[1:3], textcoords='offset points') # <--\n",
    "    \n",
    "#     if(len(hullP)<300):\n",
    "#         nrRange = [(i,p[0],p[1])  for i,p in enumerate(hullP) ]\n",
    "#         for x in nrRange:      \n",
    "#             plt.annotate('%s' % x[0], xy=x[1:3], textcoords='offset points') # <--\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Min Area Rectangle Tests\n",
    "========================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Points2DRectFail {'prec': 'double', 'name': 'Points2DRectFail', 'path': './MinAreaRectangleTest-Points2DRectFail-double.bin'} {'prec': 'double', 'name': 'Points2DRectFail', 'path': './MinAreaRectangleTest-Points2DRectFail-double-Out.bin'}\n",
      "Matrix Binary:  2 18 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 3.38826761 -2.60794988]\n",
      " [-0.39487855  0.91873333]\n",
      " [-0.91873333 -0.39487855]]\n",
      "AlmostLine {'prec': 'double', 'name': 'AlmostLine', 'path': './MinAreaRectangleTest-AlmostLine-double.bin'} {'prec': 'double', 'name': 'AlmostLine', 'path': './MinAreaRectangleTest-AlmostLine-double-Out.bin'}\n",
      "Matrix Binary: "
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/python3.4Env/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  if self._edgecolors == str('face'):\n",
      "/opt/python3.4Env/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  if self._edgecolors == str('face'):\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 2 4 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 0.          0.        ]\n",
      " [ 0.70710678  0.70710678]\n",
      " [-0.70710678  0.70710678]]\n",
      "Point {'prec': 'double', 'name': 'Point', 'path': './MinAreaRectangleTest-Point-double.bin'} {'prec': 'double', 'name': 'Point', 'path': './MinAreaRectangleTest-Point-double-Out.bin'}\n",
      "Matrix Binary:  2 1 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 1.  0.]\n",
      " [ 1.  0.]\n",
      " [ 0.  1.]]\n",
      "TwoPoints {'prec': 'double', 'name': 'TwoPoints', 'path': './MinAreaRectangleTest-TwoPoints-double.bin'} {'prec': 'double', 'name': 'TwoPoints', 'path': './MinAreaRectangleTest-TwoPoints-double-Out.bin'}\n",
      "Matrix Binary:  2 2 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 1.          0.        ]\n",
      " [ 0.5547002   0.83205029]\n",
      " [-0.83205029  0.5547002 ]]\n",
      "PointsRandom10 {'prec': 'double', 'name': 'PointsRandom10', 'path': './MinAreaRectangleTest-PointsRandom10-double.bin'} {'prec': 'double', 'name': 'PointsRandom10', 'path': './MinAreaRectangleTest-PointsRandom10-double-Out.bin'}\n",
      "Matrix Binary:  2 10 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 0.95782081  0.95453227]\n",
      " [-0.91190368 -0.41040429]\n",
      " [ 0.41040429 -0.91190368]]\n",
      "PointsRandom10M {'prec': 'double', 'name': 'PointsRandom10M', 'path': './MinAreaRectangleTest-PointsRandom10M-double.bin'} {'prec': 'double', 'name': 'PointsRandom10M', 'path': './MinAreaRectangleTest-PointsRandom10M-double-Out.bin'}\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[  9.99999818e-01   1.82427005e-07]\n",
      " [ -8.15560543e-08   1.00000000e+00]\n",
      " [ -1.00000000e+00  -8.15560543e-08]]\n",
      "PointsBadProjection2 {'prec': 'double', 'name': 'PointsBadProjection2', 'path': './MinAreaRectangleTest-PointsBadProjection2-double.bin'} {'prec': 'double', 'name': 'PointsBadProjection2', 'path': './MinAreaRectangleTest-PointsBadProjection2-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 3.16181212  5.70506863]\n",
      " [-0.99843392  0.05594372]\n",
      " [-0.05594372 -0.99843392]]\n",
      "RandomTriangle {'prec': 'double', 'name': 'RandomTriangle', 'path': './MinAreaRectangleTest-RandomTriangle-double.bin'} {'prec': 'double', 'name': 'RandomTriangle', 'path': './MinAreaRectangleTest-RandomTriangle-double-Out.bin'}\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 0.22053406  0.68698046]\n",
      " [ 0.99700658  0.07731678]\n",
      " [-0.07731678  0.99700658]]\n",
      "PointsBadProjection3 {'prec': 'double', 'name': 'PointsBadProjection3', 'path': './MinAreaRectangleTest-PointsBadProjection3-double.bin'} {'prec': 'double', 'name': 'PointsBadProjection3', 'path': './MinAreaRectangleTest-PointsBadProjection3-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 1.43898234 -5.6280445 ]\n",
      " [ 0.57332627  0.81932715]\n",
      " [-0.81932715  0.57332627]]\n",
      "UnitRectangle {'prec': 'double', 'name': 'UnitRectangle', 'path': './MinAreaRectangleTest-UnitRectangle-double.bin'} {'prec': 'double', 'name': 'UnitRectangle', 'path': './MinAreaRectangleTest-UnitRectangle-double-Out.bin'}\n",
      "Matrix Binary:  2 4 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[  1.00000000e+00   1.00000000e+00]\n",
      " [ -1.00000000e+00   2.22044605e-16]\n",
      " [ -2.22044605e-16  -1.00000000e+00]]\n",
      "PointsOnCricle1000 {'prec': 'double', 'name': 'PointsOnCricle1000', 'path': './MinAreaRectangleTest-PointsOnCricle1000-double.bin'} {'prec': 'double', 'name': 'PointsOnCricle1000', 'path': './MinAreaRectangleTest-PointsOnCricle1000-double-Out.bin'}\n",
      "Matrix Binary:  2 1000 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[  9.99999995e-01   9.98999998e-05]\n",
      " [  4.99499997e-05  -9.99999999e-01]\n",
      " [  9.99999999e-01   4.99499997e-05]]\n",
      "Triangle {'prec': 'double', 'name': 'Triangle', 'path': './MinAreaRectangleTest-Triangle-double.bin'} {'prec': 'double', 'name': 'Triangle', 'path': './MinAreaRectangleTest-Triangle-double-Out.bin'}\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[  1.00000000e+00  -1.00000000e+00]\n",
      " [  1.11022302e-16   1.00000000e+00]\n",
      " [ -1.00000000e+00   1.11022302e-16]]\n",
      "Line2 {'prec': 'double', 'name': 'Line2', 'path': './MinAreaRectangleTest-Line2-double.bin'} {'prec': 'double', 'name': 'Line2', 'path': './MinAreaRectangleTest-Line2-double-Out.bin'}\n",
      "Matrix Binary:  2 2 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 0.          0.        ]\n",
      " [ 0.70710678  0.70710678]\n",
      " [-0.70710678  0.70710678]]\n",
      "Line3 {'prec': 'double', 'name': 'Line3', 'path': './MinAreaRectangleTest-Line3-double.bin'} {'prec': 'double', 'name': 'Line3', 'path': './MinAreaRectangleTest-Line3-double-Out.bin'}\n",
      "Matrix Binary:  2 3 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 0.          0.        ]\n",
      " [ 0.70710678  0.70710678]\n",
      " [-0.70710678  0.70710678]]\n",
      "PointsBadProjection {'prec': 'double', 'name': 'PointsBadProjection', 'path': './MinAreaRectangleTest-PointsBadProjection-double.bin'} {'prec': 'double', 'name': 'PointsBadProjection', 'path': './MinAreaRectangleTest-PointsBadProjection-double-Out.bin'}\n",
      "Matrix Binary:  2 400 8 big Endian: False\n",
      "Matrix Binary:  2 7 8 big Endian: False\n",
      "[[ 1.1210723  -3.36871442]\n",
      " [-0.05435405  0.99852173]\n",
      " [-0.99852173 -0.05435405]]\n"
     ]
    }
   ],
   "source": [
    "plt.close(\"all\")\n",
    "\n",
    "files = loadFiles(\"./\" , \".*MinAreaRectangleTest-(.*?)-(\\w+).bin\", (\"name\",\"prec\") );\n",
    "filesOut = loadFiles(\"./\" , \".*MinAreaRectangleTest-(.*?)-(\\w+)-Out\\.bin\", (\"name\",\"prec\"));\n",
    "\n",
    "\n",
    "for i,f in files.items():\n",
    "    if(i in [\"NoPoint\"]): continue \n",
    "        \n",
    "    fOut = filesOut[i]; print(i,f,fOut)\n",
    "    fig = plt.figure(\"MinAreaRectangleTest\"+str(i),(10,10))\n",
    "    ax = fig.gca();\n",
    "    \n",
    "    if(i!=\"PointsRandom10M\"):\n",
    "        points = readPointsMatrixBinary(f[\"path\"]); points=np.reshape(points,(-1,2)) \n",
    "    \n",
    "    rectData =  readPointsMatrixBinary(fOut[\"path\"]); rectData=np.reshape(rectData,(-1,2))\n",
    "    \n",
    "    rect = rectData[0:4,]\n",
    "    rect = np.vstack([rect,rect[0]])\n",
    "    axis = rectData[4:,]\n",
    "    print(axis)\n",
    "    plt.plot(rect.T[0],rect.T[1],'r-', ms=20, markerfacecolor='None')\n",
    "    \n",
    "    plotAxis2d(ax,rect[0],axis[0],axis[1]);\n",
    "    \n",
    "    if(i!=\"PointsRandom10M\"):\n",
    "        plt.scatter(points.T[0],points.T[1])\n",
    "    else:\n",
    "        plt.scatter(points.T[0][0:400],points.T[1][0:400])\n",
    "    plt.axis('equal')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Diameter MVBB Tests\n",
    "========================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Plane {'path': './DiameterOOBBTest-Plane-double.bin', 'name': 'Plane', 'prec': 'double'} {'path': './DiameterOOBBTest-Plane-double-Out.txt', 'name': 'Plane', 'prec': 'double'} {'path': './DiameterOOBBTest-Plane-double-Out2.bin', 'name': 'Plane', 'prec': 'double'}\n",
      "Matrix Binary:  3 3 8 big Endian: False\n",
      "Matrix Binary:  3 2 8 big Endian: False\n",
      "UnitCube {'path': './DiameterOOBBTest-UnitCube-double.bin', 'name': 'UnitCube', 'prec': 'double'} {'path': './DiameterOOBBTest-UnitCube-double-Out.txt', 'name': 'UnitCube', 'prec': 'double'} {'path': './DiameterOOBBTest-UnitCube-double-Out2.bin', 'name': 'UnitCube', 'prec': 'double'}\n",
      "Matrix Binary:  3 8 8 big Endian: False\n",
      "Matrix Binary:  3 4 8 big Endian: False\n",
      "PointsSimulationFailMVBB {'path': './DiameterOOBBTest-PointsSimulationFailMVBB-double.bin', 'name': 'PointsSimulationFailMVBB', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsSimulationFailMVBB-double-Out.txt', 'name': 'PointsSimulationFailMVBB', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsSimulationFailMVBB-double-Out2.bin', 'name': 'PointsSimulationFailMVBB', 'prec': 'double'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "Matrix Binary:  3 400 8 big Endian: False\n",
      "PointsRandom10000 {'path': './DiameterOOBBTest-PointsRandom10000-double.bin', 'name': 'PointsRandom10000', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsRandom10000-double-Out.txt', 'name': 'PointsRandom10000', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsRandom10000-double-Out2.bin', 'name': 'PointsRandom10000', 'prec': 'double'}\n",
      "Matrix Binary:  3 10000 8 big Endian: False\n",
      "Matrix Binary:  3 400 8 big Endian: False\n",
      "PointsSimulation {'path': './DiameterOOBBTest-PointsSimulation-double.bin', 'name': 'PointsSimulation', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsSimulation-double-Out.txt', 'name': 'PointsSimulation', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsSimulation-double-Out2.bin', 'name': 'PointsSimulation', 'prec': 'double'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "Matrix Binary:  3 400 8 big Endian: False\n",
      "PointsRandom500 {'path': './DiameterOOBBTest-PointsRandom500-double.bin', 'name': 'PointsRandom500', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsRandom500-double-Out.txt', 'name': 'PointsRandom500', 'prec': 'double'} {'path': './DiameterOOBBTest-PointsRandom500-double-Out2.bin', 'name': 'PointsRandom500', 'prec': 'double'}\n",
      "Matrix Binary:  3 500 8 big Endian: False\n",
      "Matrix Binary:  3 400 8 big Endian: False\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/python3.4Env/lib/python3.4/site-packages/matplotlib/collections.py:590: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  if self._edgecolors == str('face'):\n"
     ]
    }
   ],
   "source": [
    "plt.close(\"all\")\n",
    "\n",
    "files = loadFiles(\"./\" , \".*DiameterOOBBTest-(.*?)-(\\w+)\\.bin\", (\"name\",\"prec\") );\n",
    "filesOut = loadFiles(\"./\" , \".*DiameterOOBBTest-(.*?)-(\\w+)-Out\\.txt\", (\"name\",\"prec\") );\n",
    "filesOut2 = loadFiles(\"./\" , \".*DiameterOOBBTest-(.*?)-(\\w+)-Out2\\.bin\", (\"name\",\"prec\") );\n",
    "\n",
    "for i,f in files.items():\n",
    "        \n",
    "    fOut = filesOut[i]; fOut2 = filesOut2[i]; print(i,f,fOut,fOut2)\n",
    "    fig = plt.figure(\"DiameterTest\"+str(i),(10,10))\n",
    "    \n",
    "    points = readPointsMatrixBinary(f[\"path\"]); points=np.reshape(points,(-1,3)) \n",
    "    OOBB = np.atleast_2d(np.loadtxt(fOut[\"path\"]));\n",
    "    sampled = readPointsMatrixBinary(fOut2[\"path\"]); sampled=np.reshape(sampled,(-1,3)) \n",
    "\n",
    "    K_min = OOBB[0,0:3]\n",
    "    K_max = OOBB[1,0:3]\n",
    "    A_IK = OOBB[2:,0:3]\n",
    "    center = np.zeros((3,));\n",
    "    #print(A_IK,K_min,K_max,center)\n",
    "\n",
    "    ax = Axes3D(fig)\n",
    "    if(i not in [\"PointsRandom14M\", \"Lucy\"] ):\n",
    "        ax.scatter(points.T[0],points.T[1],points.T[2],c='b')\n",
    "    else:\n",
    "        plt.scatter(points.T[0][0:2000],points.T[1][0:2000])\n",
    "    \n",
    "    ax.scatter(sampled.T[0],sampled.T[1],sampled.T[2],c='r', marker='o', s=200)\n",
    "    plotCube(ax,K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)\n",
    "    plotAxis(ax,center,A_IK,1)\n",
    "    plotAxis(ax,center,np.identity(3),0.5)\n",
    "    axisEqual3D(ax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "MVBB Tests\n",
    "========================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2017-11-12T20:47:57.540581",
     "start_time": "2017-11-12T20:47:51.926140"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PointClouds-Nr-3 {'name': 'PointClouds-Nr-3', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-3-double.bin'} {'name': 'PointClouds-Nr-3', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-3-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.70869138 -0.49319411  0.50449588]\n",
      " [ 0.63834646  0.14372591 -0.75621205]\n",
      " [ 0.3004502   0.85796412  0.41668603]] [-1.42661565 -3.86197932 -1.84620041] [ 5.34794633  0.33660258  5.23716277] [ 0.  0.  0.]\n",
      "PointClouds-Nr-2 {'name': 'PointClouds-Nr-2', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-2-double.bin'} {'name': 'PointClouds-Nr-2', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-2-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.10556901  0.99296013 -0.05371568]\n",
      " [ 0.31158544 -0.08432764 -0.94646889]\n",
      " [-0.94433559  0.08318076 -0.3182943 ]] [-2.75246443  0.47313016 -5.80166171] [ 4.34356251  3.28634465  1.24852591] [ 0.  0.  0.]\n",
      "PointClouds-Nr-0 {'name': 'PointClouds-Nr-0', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-0-double.bin'} {'name': 'PointClouds-Nr-0', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-0-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[-0.67326785  0.72861482 -0.1258207 ]\n",
      " [-0.12721436 -0.28177578 -0.95100942]\n",
      " [-0.72837278 -0.62427787  0.28240084]] [-3.07889923  0.06567176 -2.56760063] [ 1.46037201  2.70302304  1.97016091] [ 0.  0.  0.]\n",
      "PointClouds-Nr-1 {'name': 'PointClouds-Nr-1', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-1-double.bin'} {'name': 'PointClouds-Nr-1', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-1-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.71790551  0.3225827  -0.61688903]\n",
      " [-0.49581552 -0.38509148 -0.7783775 ]\n",
      " [-0.48864982  0.86466465 -0.11651779]] [-3.7118524  -1.98637348 -3.588783  ] [ 1.95426225  3.62481662 -0.85203934] [ 0.  0.  0.]\n",
      "PointClouds-Nr-5 {'name': 'PointClouds-Nr-5', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-5-double.bin'} {'name': 'PointClouds-Nr-5', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-5-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[-0.42217616 -0.81731231 -0.39213248]\n",
      " [-0.69052784  0.00968801  0.72324093]\n",
      " [-0.58731473  0.57611347 -0.56846695]] [-5.20026337 -3.69057488 -3.25279152] [ 0.02893903  1.76741518  2.60115356] [ 0.  0.  0.]\n",
      "PointClouds-Nr-4 {'name': 'PointClouds-Nr-4', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-4-double.bin'} {'name': 'PointClouds-Nr-4', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-4-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[-0.5726534  -0.60236496  0.55607962]\n",
      " [-0.59910837  0.77050971  0.21767855]\n",
      " [-0.55958668 -0.2084976  -0.80211689]] [-4.80008815 -3.87343944 -3.96441211] [ 0.69617304  2.06699221  2.01829794] [ 0.  0.  0.]\n",
      "PointClouds-Nr-9 {'name': 'PointClouds-Nr-9', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-9-double.bin'} {'name': 'PointClouds-Nr-9', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-9-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.79753714 -0.33199973 -0.50369702]\n",
      " [ 0.51391843  0.811184    0.27904905]\n",
      " [ 0.31594675 -0.48141116  0.81757015]] [-1.50647084 -3.63294126 -1.71404585] [ 5.51640073  4.3488506   2.03292811] [ 0.  0.  0.]\n",
      "PointClouds-Nr-6 {'name': 'PointClouds-Nr-6', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-6-double.bin'} {'name': 'PointClouds-Nr-6', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-6-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.40071774  0.84480005  0.35459579]\n",
      " [-0.86700551  0.47476635 -0.15132202]\n",
      " [-0.296187   -0.24679909  0.9226936 ]] [-4.10844183 -0.31695178 -0.30692678] [ 2.17617083  4.12642096  4.31170007] [ 0.  0.  0.]\n",
      "PointClouds-Nr-7 {'name': 'PointClouds-Nr-7', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-7-double.bin'} {'name': 'PointClouds-Nr-7', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-7-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[ 0.84772018  0.20510971 -0.48918351]\n",
      " [ 0.06008722  0.87914833  0.4727449 ]\n",
      " [ 0.52702943 -0.43014907  0.73294731]] [-0.39393158 -2.65702974  0.50217223] [ 3.90889747  3.6933134   5.19834765] [ 0.  0.  0.]\n",
      "PointClouds-Nr-8 {'name': 'PointClouds-Nr-8', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-8-double.bin'} {'name': 'PointClouds-Nr-8', 'prec': 'double', 'path': './MVBBTest-PointClouds-Nr-8-double-Out.txt'}\n",
      "Matrix Binary:  3 2501 8 big Endian: False\n",
      "[[-0.34365444 -0.0969246   0.93408097]\n",
      " [-0.3375783   0.94092259 -0.02656272]\n",
      " [-0.8763233  -0.32445386 -0.35607186]] [-5.02406698 -2.44769914 -2.22532513] [-0.01006527  3.0529593   2.29540345] [ 0.  0.  0.]\n"
     ]
    }
   ],
   "source": [
    "plt.close(\"all\")\n",
    "\n",
    "files = loadFiles(\"./\" , \".*MVBBTest-(.*?)-(\\w+).bin\", (\"name\",\"prec\") );\n",
    "filesOut = loadFiles(\"./\" , \".*MVBBTest-(.*?)-(\\w+)-Out\\.txt\", (\"name\",\"prec\"));\n",
    "\n",
    "\n",
    "for i,f in files.items():\n",
    "\n",
    "    fOut = filesOut[i]; \n",
    "    print(i,f,fOut);\n",
    "    fig = plt.figure(\"MVBBTest\" + str(i),(10,10))\n",
    "    \n",
    "    \n",
    "    points = readPointsMatrixBinary(f[\"path\"]); points=np.reshape(points,(-1,3)) \n",
    "    OOBB = np.atleast_2d(np.loadtxt(fOut[\"path\"]));\n",
    "\n",
    "    K_min = OOBB[0,0:3]\n",
    "    K_max = OOBB[1,0:3]\n",
    "    A_IK = OOBB[2:5,0:3]\n",
    "    center = np.zeros((3,));\n",
    "    print(A_IK,K_min,K_max,center)\n",
    "    \n",
    "    ax = Axes3D(fig)\n",
    "    if(len(points) < 10000):\n",
    "        ax.scatter(points.T[0],points.T[1],points.T[2],c='b')\n",
    "    else:\n",
    "        ax.scatter(points.T[0][0:10000],points.T[1][0:10000],points.T[2][0:10000],c='b')\n",
    "        \n",
    "    plotCube(ax,K_min,K_max,center,A_IK) # A_IK = R_KI (rotation from I to K)\n",
    "    plotAxis(ax,center,A_IK,1)\n",
    "    plotAxis(ax,center,np.identity(3),0.5)\n",
    "    axisEqual3D(ax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Initialisation Cell",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.4"
  },
  "nav_menu": {},
  "toc": {
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 6,
   "toc_cell": true,
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
